using BlockOS.Client.Res;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace BlockOS.Client
{
    public sealed class MultiplayerWindow : BaseMenuWindow
    {
        [Space]
        [SerializeField]
        private AddServerDialog addServerDialog;
        [SerializeField]
        private ServerListWidget serverListWidget;

        protected override void Awake()
        {
            base.Awake();

            inputs["Back"].performed += OnBackAction;
            inputs["AddServer"].performed += OnAddServerAction;
        }

        public override void OnShow()
        {
            base.OnShow();
            serverListWidget.Refresh();
            serverListWidget.onItemSelect += server =>
            {
                //GameClient.Instance.ShowToast($"{server} selected.");
                var ep = GameClient.Instance.ServerList.serverEntries[server];
                GameClient.Instance.SetMainStage(new RemoteWorld(ep));
            };
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            inputs["Back"].performed -= OnBackAction;
            inputs["AddServer"].performed -= OnAddServerAction;
        }

        public void OnAddServerAction(InputAction.CallbackContext _)
        {
            OpenAddServerDialog();
        }

        public void OpenAddServerDialog()
        {
            addServerDialog.modifyName = null;
            manager.PushWindow(addServerDialog, WindowManager.PushWindowMode.Addition);
        }

        private void OnBackAction(InputAction.CallbackContext obj)
        {
            manager.PopWindow(true);
        }


    }
}
